﻿using System;
using KamRemakeRemake.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace KamRemakeRemake.GameStates {
    public class MenuState : GameState {
        private Texture2D background;
        private Button playButton;

        /// <summary>
        /// Returns the center point of the current viewport
        /// </summary>
        private Vector2 ViewportCenter {
            get {
                return new Vector2(
                    this.Game.GraphicsDevice.Viewport.Bounds.Width / 2,
                    this.Game.GraphicsDevice.Viewport.Bounds.Height / 2);
            }
        }

        /// <summary>
        /// Returns the center of the backgroundimage
        /// </summary>
        private Vector2 BackgroundTextureCenter {
            get {
                return new Vector2(
                    this.background.Width / 2,
                    this.background.Height / 2);
            }
        }
        public MenuState(KaMRemake game) 
            : base (game) {
        }

        /// <summary>
        /// Event that's fired when the play game button is clicked, switch the game state to playing
        /// </summary>
        public void PlayButtonClicked(object sender, EventArgs e) {
            this.Game.SetState(new PlayingState(this.Game));
        }

        /// <summary>
        /// Draws the menu background and button
        /// </summary>
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch, ContentManager contentManager) {
            // The background image is just a enormouse texture (1920x1200), but we have to support a wide variety of 
            // screen resolutions, so we draw the menu in the center of the page
            spriteBatch.Draw(background, this.ViewportCenter, null, Color.White, 0f, this.BackgroundTextureCenter, 1f, SpriteEffects.None, 1f);

            // Tell the buttons to draw themselves
            this.playButton.Draw(gameTime, spriteBatch, contentManager);
        }

        /// <summary>
        /// Load the backgrounds content and buttons
        /// </summary>
        public override void LoadContent(ContentManager contentManager) {
            this.background = contentManager.Load<Texture2D>("sprites/gui/mainmenu");

            // Initialise play button
            this.playButton = new Button("Play", new Rectangle(
                (int)this.ViewportCenter.X,
                (int)this.ViewportCenter.Y,
                200, 50));
            this.playButton.LoadContent(contentManager);
            this.playButton.Click += new EventHandler(PlayButtonClicked);
        }

        /// <summary>
        /// Updates the buttons
        /// </summary>
        public override void Update(GameTime gameTime) {
            this.playButton.Update(gameTime);
        }
    }
}
